package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.system.controller.request.UpdateRedisReq;
import com.liuxinlong.modules.system.service.RedisManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缓存数据信息控制层
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-04-20
 */
@RestController
@RequestMapping("/redis")
public class RedisManageController {

	@Autowired
	private RedisManageService redisManageService;

	/**
	 * 查询缓存数据信息
	 * 
	 * @param req 请求信息
	 * @return 缓存数据信息列表
	 */
	@PostMapping("page")
//	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.REDIS_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageUserInfo(@RequestBody QueryBaseReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		queryMap.put("id", req.getId());
		List<Map<String, Object>> dataList = redisManageService.pageRedisInfo(queryMap);
		int totalNum = redisManageService.getRedisCount(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询缓存数据信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 删除缓存数据信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除缓存数据",business = LogBusinessConstants.REDIS_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteRedisInfo(@RequestBody BaseDeleteReq req) {
		redisManageService.deleteRedisInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除缓存数据信息成功");
		return resp;
	}

	/**
	 * 清空缓存数据信息
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("clear")
	@LogAnnotation(value ="清空缓存数据",business = LogBusinessConstants.REDIS_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp clearRedisInfo(@RequestBody UpdateRedisReq req) {
		redisManageService.clearRedisInfo(req.getId(),req.getSubKey());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("清空缓存数据信息成功");
		return resp;
	}

	/**
	 * 查询缓存数据详情信息
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("detail")
	public BaseResp queryRedisDetailInfo(@RequestBody QueryBaseReq req) {
		List<Map<String,Object>> dataList = redisManageService.queryRedisDetailInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setDataList(dataList);
		resp.setTotalNum(dataList.size());
		resp.setRetMessage("查询缓存数据详情信息成功");
		return resp;
	}
}
